package com.myapp.struts.menu;

import com.myapp.struts.form.MenuForms;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class JavaToHTML extends MenuFormat {

    File f;
    private static JavaToHTML html;

    public JavaToHTML getInstance() {
        if (html == null) {
            html = new JavaToHTML();
            return html;
        } else {
            return html;
        }
    }
    //arraylist of fooditems

    public String formatHTML(ArrayList<FoodItems> foodItem, String outFile) throws Exception {

        f = new File(outFile, "Menu_HTML.htm");
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("<html>");
        bw.write("<body>");

        printHTML(foodItem, bw, Constants.BREAKFAST);
        printHTML(foodItem, bw, Constants.SNACK);
        printHTML(foodItem, bw, Constants.APPETIZER);
        printHTML(foodItem, bw, Constants.LUNCH);
        printHTML(foodItem, bw, Constants.DINNER_FOOD);
        printHTML(foodItem, bw, Constants.DESSERT);
        printHTML(foodItem, bw, Constants.SIDE_DISH);


        
        bw.write("</body>");
        bw.write("</html>");

        bw.close();

        System.out.println("file path" + f.getAbsolutePath());
        return f.getAbsolutePath();

    }

    private void printHTML(ArrayList<FoodItems> foodItem, BufferedWriter bw, String type)
            throws IOException {
        Boolean head = false;
        for (FoodItems foodItems : foodItem) {

            if (foodItems.getCategory().equals(type)) {
                if (head == false) {
                    bw.write("<h1>" + type + "</h1>");
                    head = true;
                }
                bw.write("<br>");
                bw.write("<li>");
                bw.write(foodItems.getName());
                bw.write("</li>");
                bw.write("<i>" + foodItems.getDescription() + "</i>");
                bw.write("<br>");
                if (foodItems.getCountry().equals(Constants.GREAT_BRITAN)) {
                    bw.write("GBP " + foodItems.getPrice());
                } else {
                    bw.write("$ " + foodItems.getPrice());
                }
                bw.write("<br>");

            }

        }
    }

    public File getF() {
        return f;
    }

    public void setF(File f) {
        this.f = f;
    }
}
